package com.bdult.ozworld.controller;

import java.util.HashMap;
import java.util.Locale;
import java.util.Map;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.bdult.ozworld.common.BreadcrumbUtil;
import com.bdult.ozworld.service.UserService;
import com.bdult.ozworld.common.Constants;

@Controller
public class MemberController {

	@Autowired
	private UserService userService;

	@Autowired
	private BreadcrumbUtil breadcrumbUtil;
	
	@Autowired
	private MessageSource messageSource;
	
	@RequestMapping(value="loginpage.do")
	public ModelAndView loginpage() {
		ModelAndView mv = new ModelAndView();
		mv.setViewName("member/loginpage");
		mv.addObject(Constants.BREADCRUMBS, breadcrumbUtil.getBreadcrumbs(Constants.BREADCRUMB_HOME, Constants.BREADCRUMB_LOGIN));
		return mv;
	}
	
	@RequestMapping(value = "loginResult.do", method = RequestMethod.POST)
	public String loginResult(Model model, @RequestParam Map<String, Object> paramMap, 
			HttpServletRequest request, HttpServletResponse response, HttpSession session, Locale locale) {
		model.addAttribute(Constants.BREADCRUMBS, breadcrumbUtil.getBreadcrumbs(Constants.BREADCRUMB_HOME, Constants.BREADCRUMB_LOGIN));
		
		HashMap<String, String> sessionMap = (HashMap<String, String>) userService.detail(paramMap);
		
		if(sessionMap == null) {
			System.out.println("getlocale = " + request.getLocale());
			String msg = messageSource.getMessage("login.alert", null, request.getLocale());
			System.out.println("msg = " + msg);
			model.addAttribute("msg", msg);
			return "member/loginpage";
		} else {
			if(session.getAttribute("userInfoSession") == null){
				session.setAttribute("userInfoSession", sessionMap);
				response.addCookie(new Cookie("userIdCookie", paramMap.get("id").toString()));
				response.addCookie(new Cookie("userPwdCookie", paramMap.get("pwd").toString()));
				if(paramMap.get("userSaveId") != null){
					response.addCookie(new Cookie("userIdCheck", paramMap.get("userSaveId").toString()));
				}else{
					response.addCookie(new Cookie("userIdCheck", ""));
				}
				if(paramMap.get("userSavePw") != null){
					response.addCookie(new Cookie("userPwdCheck", paramMap.get("userSavePw").toString()));
				}else {
					response.addCookie(new Cookie("userPwdCheck", ""));
				}
			}
			return "member/loginResult";
		}
		
	}
	
	@RequestMapping(value = "joinProvision.do", method = RequestMethod.GET)
	public ModelAndView joinProvision(Model model) {
		ModelAndView mav = new ModelAndView();
		mav.setViewName("member/joinProvision");
		mav.addObject(Constants.BREADCRUMBS, breadcrumbUtil.getBreadcrumbs(Constants.BREADCRUMB_HOME, Constants.BREADCRUMB_JOINPROVISION));
		return mav;
	}
	
	@RequestMapping(value = "signUp.do", method = RequestMethod.GET)
	public ModelAndView signUp(Model model) {
		ModelAndView mav = new ModelAndView();
		mav.setViewName("member/signUp");
		mav.addObject(Constants.BREADCRUMBS, breadcrumbUtil.getBreadcrumbs(Constants.BREADCRUMB_HOME, Constants.BREADCRUMB_JOINPROVISION, Constants.BREADCRUMB_SIGNUP));
		
		
		return mav;
	}

	@RequestMapping(value = "signUpResult.do", method = RequestMethod.POST)
	public ModelAndView signUpResult(Model model, @RequestParam Map<String, Object> paramMap) {
		ModelAndView mav = new ModelAndView();
		mav.setViewName("member/signUpResult");
		mav.addObject(Constants.BREADCRUMBS, breadcrumbUtil.getBreadcrumbs(Constants.BREADCRUMB_HOME, Constants.BREADCRUMB_JOINPROVISION, Constants.BREADCRUMB_SIGNUP, Constants.BREADCRUMB_SIGNUPRESULT));
System.out.println("*****" + paramMap);		
		userService.insertUser(paramMap);
		
		return mav;
	}
}
